﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace RISMOTAXDP.EmailService
{
    /// <summary>
    /// 邮件后台服务
    /// </summary>
    public partial class EmailService : ServiceBase
    {

        /// <summary>
        /// 守护进程对象
        /// </summary>
        private GuardThread m_GuardThread;

        public EmailService()
        {
            InitializeComponent();
            base.CanPauseAndContinue = true;
        }

        #region 服务控制
        /// <summary>
        /// 后台服务启动处理
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {

            //1、初始化全局对象
            GlobalObject.Initialize();

            //2、创建邮件生成线程
            MailCreateThread createThread = new MailCreateThread();

            //3、创建邮件收发线程
            MailTransferThread transferThread = new MailTransferThread();

            //NewsletterProcess线程
            MailNewsletterProcessThread newsletterProcessThread = new MailNewsletterProcessThread();

            //4、创建系统守护线程
            m_GuardThread = new GuardThread(createThread, transferThread, newsletterProcessThread);

            //5、修改系统状态
            GlobalObject.State = ServiceState.Running;

            //6、记录系统守护线程
            m_GuardThread.Start();

            //7、记录系统停止日志
            GlobalObject.WriteSystemLog("S00000001", null);

        }

        /// <summary>
        /// 后台服务停止处理
        /// </summary>
        protected override void OnStop()
        {
            //1、修改系统状态
            GlobalObject.State = ServiceState.Stopped;

            //2、停止系统守护线程
            m_GuardThread.Stop();

            //3、记录系统停止日志
            GlobalObject.WriteSystemLog("S00000002", null);

        }

        /// <summary>
        /// 后台服务暂停处理
        /// </summary>
        protected override void OnPause()
        {
            //1、修改系统状态
            GlobalObject.State = ServiceState.Paused;
            //2、记录系统暂停日志

            GlobalObject.WriteSystemLog("S00000003", null);
            base.OnPause();
        }

        /// <summary>
        /// 后台服务继续处理
        /// </summary>
        protected override void OnContinue()
        {
            GlobalObject.ReInitialize();

            //1、修改系统状态

            GlobalObject.State = ServiceState.Running;
            //2、记录系统继续日志

            GlobalObject.WriteSystemLog("S00000004", null);
            base.OnContinue();
        }

        #endregion

        #region 控制台测试
        /// <summary>
        /// 控制台测试函数

        /// </summary>
        public void ConsoleStart()
        {
            this.OnStart(null);
        }

        /// <summary>
        /// 控制台测试函数

        /// </summary>
        public void ConsoleStop()
        {
            this.OnStop();
        }

        #endregion
    }
}
